//
//  KLMAlertController.swift
//  KLM
//
//  Created by 朱雨 on 2022/5/27.
//

import Foundation

class KLMAlertController {
    
    var alertController: UIAlertController?
    
    static let shared = KLMAlertController()
    private init(){}
    
    /// 弹出提示框
    /// - Parameters:
    ///   - title: title
    ///   - message: message
    static func showAlertWithTitle(title: String?, message: String?, viewController: UIViewController? = nil, sure: (() -> ())? = nil, cancel: (() -> ())? = nil) {
        
        let alert = KLMAlertController.shared
        if let currentAlert =  alert.alertController {
            currentAlert.dismiss(animated: true)
        }
        let aler = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
        alert.alertController = aler
        let sure = UIAlertAction.init(title: KLMLocalizable.LANGLOC("Confirm"), style: .default, handler: { action in
            
            if let ss = sure {
                ss()
            }
        })
        aler.addAction(sure)
        if let cancel = cancel {
            let can = UIAlertAction.init(title: KLMLocalizable.LANGLOC("Cancel"), style: .cancel, handler: { action in
                cancel()
            })
            aler.addAction(can)
        }
        if let viewController = viewController {
            viewController.present(aler, animated: true, completion: nil)
        } else {
            KLMKeyWindow?.rootViewController?.present(aler, animated: true, completion: nil)
        }
    }
}
